package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-12 14:17
 */
public class Solution_0577 {
    public String reverseWords(String s) {
        String[] s1 = s.split(" ");
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < s1.length; i++) {
            if (i != s1.length - 1) {
                res.append(this.reverseString(s1[i]) + " ");
            } else {
                res.append(this.reverseString(s1[i]));
            }

        }
        return res.toString();
    }

    /**
     * 反转字符串
     */
    public String reverseString(String param) {
        char[] s = param.toCharArray();
        for (int i = 0, j = s.length - 1; i < j; i++, j--) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
        }
        return new String(s);
    }
}
